<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class jr_web_fetcher_entry extends JR_Module {
	public $module_folder_name = "jr_web_fetcher";
	public function index()
	{
		$this->load->library('ex_snoopy');
		//echo $this->ex_snoopy->fetch("http://www.qq.com");
		$this->load->view('admin/v_entry') ; 
	}
	public function edit(){
		$web_fetcher = $this->build_class('web_fetcher');
		if(!empty($_GET['id']))
		{
			$web_fetcher = 
					$this->db->get_where('web_fetcher',array('id'=>$this->input->get('id')))->row();
		}
		$web_fetcher->error = Array();
		
		if(!empty($_POST))   // 假如提交了
		{
			$error = Array();
			if($_POST['p_title']==null )
			{
				$error['p_title'] = "采集器名不能为空！";
			}
			if(!preg_match('/^(http|https|ftp):\/\/([A-Z0-9][A-Z0-9_-]*(?:\.[A-Z0-9][A-Z0-9_-]*)+):?(\d+)?\/?/i', $_POST['p_url']))
			{
				$error['p_url'] = "请输入规范的网址！";
			}
			if($_POST['p_start'] == null )
			{
				$error['p_start'] = "请设置采集起始点";
			}
			if($_POST['p_end'] == null )
			{
				$error['p_end'] = "请设置采集结束点";
			}
			if($_POST['p_link_parse_regular'] == null )
			{
				$error['p_link_parse_regular'] = "请输入列表页链接规则";
			}
			//if($_POST['p_page_charset'] == null )
			//{
			//	$error['p_page_charset'] = "请设置文字编码规则";
			//}
			if($_POST['p_page_start'] == null )
			{
				$error['p_page_start'] = "请设置单页起始点";
			}
			if($_POST['p_page_end'] == null )
			{
				$error['p_page_end'] = "请设置单页结束点";
			}
			if($_POST['p_title_regular'] == null )
			{
				$error['p_title_regular'] = "请设置单页内待采集标题规则";
			}
			if($_POST['p_datetime_regular'] == null )
			{
				$error['p_datetime_regular'] = "请设置单页内待采集发表时间规则";
			}
			if($_POST['p_from_regular'] == null )
			{
				$error['p_from_regular'] = "请设置单页内待采集来源规则";
			}
			if($_POST['p_content_regular'] == null )
			{
				$error['p_content_regular'] = "请设置单页内待采集内容规则";
			}
			if($_POST['p_author_regular'] == null )
			{
				$error['p_author_regular'] = "请设置单页内待采集作者规则";
			}
			if($_POST['p_page_class'] == null )
			{
				$error['p_page_class'] = "请设置插入数据库文章所属分类名";
			}
			if(sizeof($error)==0)
			{
				if(empty($_GET['id']))  // 假如是添加
				{
					$this->db->insert('web_fetcher',$this->filter_post());
					redirect(SELF."/module/jr_web_fetcher");
				}
				else 
				{
					$post_web_fetcher = $this->filter_post();
					$this->db->update('web_fetcher',$post_web_fetcher,array('id'=>$this->input->get('id')));
					redirect(SELF."/module/jr_web_fetcher");
					//print_r($post_web_fetcher);
					//exit;
				}
			}
			else
			{
				$web_fetcher->error = $error;
			}
		}
		
		$this->load->view('admin/v_form',$web_fetcher) ;
	}
	
}
